package com.example.meituan.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.example.meituan.activity.bean.ShopBean;
import com.example.meituan.db.DBUntil;

public class ShopDao {

    public static SQLiteDatabase db;

    public ShopDao(Context context){
        DBUntil dbUntil=new DBUntil(context);
        DBUntil.con=dbUntil.getWritableDatabase();
        db= DBUntil.con;
    }

    // 添加商品
    public static  int addShop(ShopBean shop) {
        ContentValues values = new ContentValues();
        values.put("s_id", shop.getId());
        values.put("s_business_id", shop.getBusinessId());
        values.put("s_name", shop.getName());
        values.put("s_des", shop.getDes());
        values.put("s_price", shop.getPrice());
        values.put("s_type",shop.getType());
        values.put("s_time",shop.getTime());
        try {
            db.insert("d_shop", null, values);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    // 删除商品
    public static int deleteShopById(String id) {
        try {
            db.delete("d_shop", "s_id=?", new String[]{id});
            db.delete("d_type","s_shop_id=?",new String[]{id});
            db.delete("d_img","s_shop_id",new String[]{id});
            return 1;
        }
        catch(Exception e){
            return 0;
        }
    }
    // 更新商品
    public static int updateShop(ShopBean shop) {
        ContentValues values = new ContentValues();
        values.put("s_business_id", shop.getBusinessId());
        values.put("s_name", shop.getName());
        values.put("s_des", shop.getDes());
        values.put("s_price", shop.getPrice());
        values.put("s_type",shop.getType());
        values.put("s_time",shop.getTime());
        try {
            db.update("d_shop", values, "s_id=?", new String[]{shop.getId()});
            return 1;
        }
        catch (Exception e)
        {
            return 0;
        }
    }

    // 商家查询所有商品
    public List<ShopBean> getAllShopsByBusinessId(String businessId) {
        List<ShopBean> list=new ArrayList<>();
        Cursor cursor=db.rawQuery("select * from d_shop where s_business_id=?",new String[]{businessId});
        while (cursor.moveToNext()){
            @SuppressLint("Range") String id=cursor.getString(cursor.getColumnIndex("s_id"));
            @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("s_name"));
            @SuppressLint("Range") String des=cursor.getString(cursor.getColumnIndex("s_des"));
            @SuppressLint("Range") String price=cursor.getString(cursor.getColumnIndex("s_price"));
            @SuppressLint("Range") String type=cursor.getString(cursor.getColumnIndex("s_type"));
            @SuppressLint("Range") String time=cursor.getString(cursor.getColumnIndex("s_time"));
            ShopBean shopBean=new ShopBean();
            shopBean.setId(id);
            shopBean.setTime(Long.parseLong(time));
            shopBean.setDes(des);
            shopBean.setName(name);
            shopBean.setBusinessId(businessId);
            shopBean.setPrice(price);
            shopBean.setType(type);
            list.add(shopBean);
        }
        cursor.close();
        return list;
    }

    public List<ShopBean> getAllShopsByTime(String businessId) {
        List<ShopBean> list=new ArrayList<>();
        long oneMonthAgo = System.currentTimeMillis() - (30L * 24 * 60 * 60 * 1000);
        String query = "SELECT * FROM d_shop WHERE s_business_id = ? AND s_time >= ? ORDER BY s_time DESC";
        Cursor cursor = db.rawQuery(query, new String[]{businessId, String.valueOf(oneMonthAgo)});
        while (cursor.moveToNext()){
            @SuppressLint("Range") String id=cursor.getString(cursor.getColumnIndex("s_id"));
            @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("s_name"));
            @SuppressLint("Range") String des=cursor.getString(cursor.getColumnIndex("s_des"));
            @SuppressLint("Range") String price=cursor.getString(cursor.getColumnIndex("s_price"));
            @SuppressLint("Range") String type=cursor.getString(cursor.getColumnIndex("s_type"));
            @SuppressLint("Range") String time=cursor.getString(cursor.getColumnIndex("s_time"));
            ShopBean shopBean=new ShopBean();
            shopBean.setId(id);
            shopBean.setTime(Long.parseLong(time));
            shopBean.setDes(des);
            shopBean.setName(name);
            shopBean.setBusinessId(businessId);
            shopBean.setPrice(price);
            shopBean.setType(type);
            list.add(shopBean);
        }
        cursor.close();
        return list;
    }

    public List<ShopBean> getAllShopsByNum(String businessId) {
        List<ShopBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from d_shop where s_business_id=?", new String[]{businessId});

        while (cursor.moveToNext()) {
            @SuppressLint("Range") String id = cursor.getString(cursor.getColumnIndex("s_id"));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("s_name"));
            @SuppressLint("Range") String des = cursor.getString(cursor.getColumnIndex("s_des"));
            @SuppressLint("Range") String price = cursor.getString(cursor.getColumnIndex("s_price"));
            @SuppressLint("Range") String type = cursor.getString(cursor.getColumnIndex("s_type"));
            @SuppressLint("Range") String time=cursor.getString(cursor.getColumnIndex("s_time"));
            ShopBean shopBean = new ShopBean();
            shopBean.setId(id);
            shopBean.setTime(Long.parseLong(time));
            shopBean.setDes(des);
            shopBean.setName(name);
            shopBean.setBusinessId(businessId);
            shopBean.setPrice(price);
            shopBean.setType(type);

            int a = getMonSale(shopBean.getId());

            list.add(shopBean);
        }
        cursor.close();

        Collections.sort(list, new Comparator<ShopBean>() {
            @Override
            public int compare(ShopBean o1, ShopBean o2) {
                return Integer.compare(getMonSale(o1.getId()), getMonSale(o2.getId())); // 降序排列
            }
        });

        return list;
    }

    public List<ShopBean> getAllShopsByType(String type) {
        List<ShopBean> list=new ArrayList<>();
        Cursor cursor=db.rawQuery("select * from d_shop where s_type=?",new String[]{type});
        while (cursor.moveToNext()){
            @SuppressLint("Range") String id=cursor.getString(cursor.getColumnIndex("s_id"));
            @SuppressLint("Range") String businessId=cursor.getString(cursor.getColumnIndex("s_business_id"));
            @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("s_name"));
            @SuppressLint("Range") String des=cursor.getString(cursor.getColumnIndex("s_des"));
            @SuppressLint("Range") String price=cursor.getString(cursor.getColumnIndex("s_price"));
            @SuppressLint("Range") String time=cursor.getString(cursor.getColumnIndex("s_time"));
            ShopBean shopBean=new ShopBean();
            shopBean.setId(id);
            shopBean.setDes(des);
            shopBean.setName(name);
            shopBean.setBusinessId(businessId);
            shopBean.setPrice(price);
            shopBean.setTime(Long.parseLong(time));
            shopBean.setType(type);
            list.add(shopBean);
        }
        cursor.close();
        return list;
    }

    //用户查询所有商品
    public List<ShopBean> getAllShops() {
        List<ShopBean> list=new ArrayList<>();
        Cursor cursor=db.rawQuery("select * from d_shop ",null);
        while (cursor.moveToNext()){
            @SuppressLint("Range") String id=cursor.getString(cursor.getColumnIndex("s_id"));
            @SuppressLint("Range") String businessId=cursor.getString(cursor.getColumnIndex("s_business_id"));
            @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("s_name"));
            @SuppressLint("Range") String des=cursor.getString(cursor.getColumnIndex("s_des"));
            @SuppressLint("Range") String price=cursor.getString(cursor.getColumnIndex("s_price"));
            @SuppressLint("Range") String type=cursor.getString(cursor.getColumnIndex("s_type"));
            @SuppressLint("Range") String time=cursor.getString(cursor.getColumnIndex("s_time"));
            ShopBean shopBean=new ShopBean();
            shopBean.setId(id);
            shopBean.setDes(des);
            shopBean.setName(name);
            shopBean.setTime(Long.parseLong(time));
            shopBean.setBusinessId(businessId);
            shopBean.setPrice(price);
            shopBean.setType(type);
            list.add(shopBean);
        }
        cursor.close();
        return list;
    }

    // 根据ID查询单个商品
    public static ShopBean getShopById(String id) {
        ShopBean shopBean=new ShopBean();
        Cursor cursor=db.rawQuery("select * from d_shop where s_id=?",new String[]{id});
       if (cursor.moveToNext()){
            @SuppressLint("Range") String businessId=cursor.getString(cursor.getColumnIndex("s_business_id"));
            @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("s_name"));
            @SuppressLint("Range") String des=cursor.getString(cursor.getColumnIndex("s_des"));
            @SuppressLint("Range") String price=cursor.getString(cursor.getColumnIndex("s_price"));
           @SuppressLint("Range") String type=cursor.getString(cursor.getColumnIndex("s_type"));
           @SuppressLint("Range") String time=cursor.getString(cursor.getColumnIndex("s_time"));
            shopBean.setId(id);
            shopBean.setDes(des);
            shopBean.setName(name);
            shopBean.setTime(Long.parseLong(time));
            shopBean.setBusinessId(businessId);
            shopBean.setType(type);
            shopBean.setPrice(price);
        }else{
            return null;
       }
       cursor.close();
        return shopBean;
    }


    //获取月售
    public static int getMonSale(String id){
        Cursor cursor=db.rawQuery("SELECT  * FROM d_orders WHERE strftime(\"%Y-%m\", s_order_time) = strftime(\"%Y-%m\", \"now\") And s_id=?;",new String[]{id});
        int count = 0;
        if(cursor!=null){
            while (cursor.moveToNext()){
                @SuppressLint("Range") String orderDetialsId=cursor.getString(cursor.getColumnIndex("s_order_detials_id"));
                Cursor cursor1=db.rawQuery("SELECT * FROM d_order_detials_id WHERE s_id=?;",new String[]{orderDetialsId});
                if(cursor1.moveToNext()){
                    @SuppressLint("Range") String num=cursor1.getString(cursor1.getColumnIndex("s_num"));
                    count+=Integer.parseInt(num);
                }
            }
            cursor.close();
        }
        return count;
    }

    public List<ShopBean> getAllShopsByName(String businessId, String n) {
        List<ShopBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("SELECT * FROM d_shop WHERE s_business_id = ? AND s_name LIKE ? or s_type LIKE ?",
                new String[]{businessId, "%" + n + "%","%" + n + "%"});

        while (cursor.moveToNext()) {
            @SuppressLint("Range") String id = cursor.getString(cursor.getColumnIndexOrThrow("s_id"));
            @SuppressLint("Range") String des = cursor.getString(cursor.getColumnIndexOrThrow("s_des"));
            @SuppressLint("Range") String priceStr = cursor.getString(cursor.getColumnIndexOrThrow("s_price"));
            @SuppressLint("Range") String type = cursor.getString(cursor.getColumnIndexOrThrow("s_type"));
            @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("s_name"));
            @SuppressLint("Range") String time=cursor.getString(cursor.getColumnIndex("s_time"));
            ShopBean shopBean = new ShopBean();
            shopBean.setId(id);
            shopBean.setTime(Long.parseLong(time));
            shopBean.setDes(des);
            shopBean.setName(cursor.getString(cursor.getColumnIndexOrThrow("s_name")));
            shopBean.setBusinessId(businessId);
            shopBean.setPrice(priceStr);
            shopBean.setType(type);
            list.add(shopBean);
        }

        cursor.close();
        return list;
    }

    public List<ShopBean> getAllShopsByNameType( String n) {
        List<ShopBean> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("SELECT * FROM d_shop WHERE  s_name LIKE ? or s_type LIKE ? or s_des LIKE ? ",
                new String[]{ "%" + n + "%","%" + n + "%","%" + n + "%"});

        while (cursor.moveToNext()) {
            @SuppressLint("Range") String id=cursor.getString(cursor.getColumnIndex("s_id"));
            @SuppressLint("Range") String businessId=cursor.getString(cursor.getColumnIndex("s_business_id"));
            @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("s_name"));
            @SuppressLint("Range") String des=cursor.getString(cursor.getColumnIndex("s_des"));
            @SuppressLint("Range") String price=cursor.getString(cursor.getColumnIndex("s_price"));
            @SuppressLint("Range") String type=cursor.getString(cursor.getColumnIndex("s_type"));
            @SuppressLint("Range") String time=cursor.getString(cursor.getColumnIndex("s_time"));
            ShopBean shopBean=new ShopBean();
            shopBean.setId(id);
            shopBean.setTime(Long.parseLong(time));
            shopBean.setDes(des);
            shopBean.setName(name);
            shopBean.setBusinessId(businessId);
            shopBean.setPrice(price);
            shopBean.setType(type);
            list.add(shopBean);
        }

        cursor.close();
        return list;
    }
}
